//
//  HLCategoryViewController.swift
//  HLXiMaLaYa
//
//  Created by hulinhu on 2018/1/29.
//  Copyright © 2018年 hulinhu. All rights reserved.

//  首页分类

import UIKit
import ReusableKit
import RxDataSources
import ObjectMapper


class HLCategoryViewController: HLBaseViewController {
    
    
    // viewModel
    private var viewModel = HLCategoryViewModel()
    private var vmInput : HLCategoryViewModel.HLCategoryInput?
    private var vmOutput: HLCategoryViewModel.HLCategoryOutput?

    // View
    private var collectionView: UICollectionView!
    var dataSource : RxCollectionViewSectionedReloadDataSource<HLCategorySection>!

    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        bindUI()
    }
    
    // 显示 播放按钮
    override func viewWillAppear(_ animated: Bool) {
        if let tabbarVC = self.tabBarController as? HLTabBarViewController {
            tabbarVC.isHiddenPlayView(false)
        }
    }
}

extension HLCategoryViewController {
    
    func initUI()  {
        let layOut =  HLCategoryFlowLayout()
        let collectionView = UICollectionView.init(frame: view.bounds, collectionViewLayout: layOut).then {
            $0.backgroundColor = UIColor.white
            $0.showsVerticalScrollIndicator = false
            view .addSubview($0)
            $0.snp.makeConstraints { (make) in
                make.left.top.right.bottom.equalToSuperview()
            }
            // 设置代理
            $0.rx.setDelegate(self as UICollectionViewDelegate ).disposed(by: rx.disposeBag)
            
            // 单元格Cell
            $0.register(Reusable.collectionCell)
            $0.register(Reusable.collectionHeardView , kind: SupplementaryViewKind.header)
            $0.register(Reusable.collectionFooterView, kind: SupplementaryViewKind.footer)

        }
        
      self.collectionView = collectionView
    }
    
    func bindUI()  {
        dataSource = RxCollectionViewSectionedReloadDataSource<HLCategorySection>.init(configureCell: { (dataSource, collectionView, indexPath, model) -> UICollectionViewCell in
            let cell = collectionView.dequeue(Reusable.collectionCell, for: indexPath)
            cell.imageV.image = UIImage.init(named: model.imageName)
            cell.titleLabel.text = model.titleName
            
            return cell
        }, configureSupplementaryView: { (dataSection, collectionView, kind, indexPath) in
            let dsSection = dataSection[indexPath.section]
            
            if kind == UICollectionElementKindSectionHeader {
                let sectionHeader = collectionView.dequeue(Reusable.collectionHeardView, kind: .header, for: indexPath)
                return sectionHeader
                
            }else {
                let sectionFooter = collectionView.dequeue(Reusable.collectionFooterView, kind: .footer , for: indexPath)
                sectionFooter.imageView.backgroundColor = UIColor.yellow
                return sectionFooter
            }
        })
        
        
        collectionView.rx.modelSelected(HLCategoryItemModel.self).subscribe(onNext: {[weak self] (model) in
            guard let `self` = self else { return }
            self.jumpDetailVC()
        }).disposed(by: rx.disposeBag)
        
        
      // 进行初始化他
      vmInput =  HLCategoryViewModel.HLCategoryInput(category: .Category)
      vmOutput = viewModel.transform(input: vmInput!)
       // 监听进行数据绑定 重要
      vmOutput?.sections.asDriver().drive(collectionView.rx.items(dataSource: dataSource)).disposed(by: rx.disposeBag)
       // 进行请求数据
      vmOutput?.requestCommand.onNext(true)

    }
}
// MARK:- 布局
extension HLCategoryViewController:UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return HLCategoryCollectionViewCell.itemSize()
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return HLCategoryHeardView.itemSize()
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return HLCategoryFooterView.itemSize()
    }
}
// MARK: -  cell
private enum Reusable {
    
    static let collectionCell = ReusableCell<HLCategoryCollectionViewCell>(nibName: "HLCategoryCollectionViewCell")
    
    static let collectionHeardView = ReusableView<HLCategoryHeardView>(nibName: "HLCategoryHeardView")
    
    static let collectionFooterView = ReusableView<HLCategoryFooterView>(nibName: "HLCategoryFooterView")

}

// MARK:- 控制器跳转
extension HLCategoryViewController {
    
    // MARK:-  详情
    func jumpDetailVC() {
   
        let VC = HLDetailViewController()
        VC.hidesBottomBarWhenPushed = true
        if let tabbarVC = self.tabBarController as? HLTabBarViewController {
            tabbarVC.isHiddenPlayView(true)
        }
        self.navigationController?.pushViewController(VC, animated: true)
    }
}


